package com.proscenic.oauth.server.config.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义授权码的生成
 */
@Slf4j
@Service
public class MyAuthorizationCodeServices implements org.springframework.security.oauth2.provider.code.AuthorizationCodeServices {
    protected final ConcurrentHashMap<String, OAuth2Authentication> authorizationCodeStore = new ConcurrentHashMap<String, OAuth2Authentication>();

    @Override
    public String createAuthorizationCode(OAuth2Authentication authentication) {
        authorizationCodeStore.put("123456", authentication);
        return "123456";
    }

    @Override
    public OAuth2Authentication consumeAuthorizationCode(String code) throws InvalidGrantException {
        log.info("用户提交授权码：" + code);
        return authorizationCodeStore.get("123456");
    }
}
